import React from 'react';
import {Button, message, Modal, Spin} from 'antd';
import keDaScanService from 'components/keDaScanService';
import {eventTrack, messages} from 'share/common';
import 'styles/scan-wrap/index.scss'
import menuRoute from 'routes/menuRoute';
import mainService from 'containers/main.service';

// 柯达扫描仪连接
// 需要先安装服务
class KeDaScan extends React.Component {

  constructor(props) {
    super(props);
    this.state = {
      disabled: true,
      isScan: false
    }
  }

  componentDidMount() {
    // 关闭处理
    console.log("柯达扫描仪。。。。。。。")
    keDaScanService.setOnClose(() => {
      this.setState({ disabled: true, isScan: false });
    });
    // 开启处理
    keDaScanService.setOpenCallback(() => {
      this.setState({ disabled: false });
    });
    // 扫描获取信息处理
    keDaScanService.setScanSuccessHandle((src) => {
      this.setState({ isScan: false });
      let base64 = src.split(';base64,')[1];
      if (base64) {
        this.props.onScan({
          imageData: base64
        });
      }
    });
    // 错误处理
    keDaScanService.setErrorHandle((code, params) => {
      message.error(this.getErrorMessage(code, params));
      this.props.onEventTrack({errorInfo: this.getErrorMessage(code, params)});
    });
    keDaScanService.init();
  }
  getErrorMessage = (code, params) => {
     return {
       close: messages('common-1.key74')/*连接已关闭, 请重启服务或刷新当前页面!*/,
       loading: messages('common-1.key75',{arg1: params})/*请等待当前操作完成:{arg1}!*/
     }[code] || messages('common-1.key74')/*连接已关闭, 请重启服务或刷新当前页面!*/;
  };
  openInvoiceInfoAuthority() {
    window.open(`${window.ISIE9 ? "#" : ""}${menuRoute.getRouteItem('invoice-source-check').url}`, '_blank');
  }
  getOpenInvoiceInfoAuthority = () => {
    return mainService.checkMenuAuth(menuRoute.getRouteItem('invoice-source-check','key'));
  };
  getShowExpenseExtra(str,  linkBtn, specialStr) {
    let showExtra = str.split(specialStr);
    return ( <div>
      {showExtra[0]}{linkBtn}{showExtra[1]}
    </div>)
  };
  capture = () => {
    const {showOCRFlag} = this.props;
    if (!showOCRFlag) {
      let replaceInfo = messages('common-1.key1236')/*发票来源与查验*/;
      let linkBtn = (<a onClick={()=>this.openInvoiceInfoAuthority()}>{replaceInfo}</a>);
      let infoMessage = messages('common-1.key1237',{arg1: replaceInfo})/*请管理员在{arg1}菜单，开通发票识别服务*/;
      let openInvoiceAuthority = this.getOpenInvoiceInfoAuthority();
      Modal.info({
        title: messages('common-1.key71')/*说明*/,
        content: openInvoiceAuthority ? this.getShowExpenseExtra(infoMessage, linkBtn, replaceInfo) : infoMessage
      });
      this.props.onEventTrack({notOCRFlag: true});
      return !1;
    }
    if (this.state.disabled) {
      const labels = [
       messages('common-1.key1238')/*1. 扫描仪已和电脑保持连接，并且扫描仪处于开机状态（扫描仪型号要求：佳能DR-M1060、佳能DR-M140、富士通IX1500L、富士通IX500）*/,
       messages('common-1.key1239')/*2. 驱动安装完毕，并且在任务管理器- 进程中为启动状态。各型号对应进程名称：佳能 ScanServer 、富士通 PfuSsService*/,
      ];
      Modal.info({
        title: messages('common-1.key1240')/*请确保已完成以下操作*/,
        content: (
          <section>
            {labels.map(item => <p>{item}</p>)}
          </section>
        )
      });
      this.props.onEventTrack({disabled: true});
      return !1;
    }
    if (this.state.isScan) {
      return !1;
    }
    this.setState({ isScan: true });
    this.props.onStartScan();
    keDaScanService.startScan();
  };

  render() {
    const { disabled, isScan } = this.state;
    const {text, buttonProps} = this.props;
    let wrapButtonProps = {...buttonProps};
    wrapButtonProps.loading = !!buttonProps.loading || isScan;
    if (disabled) {
      wrapButtonProps.type = 'default';
    }
    return (
        <Button onClick={this.capture}
                {...wrapButtonProps}>
          {text || messages('common-1.key31')/*扫描*/}
        </Button>
    );
  }
}
KeDaScan.propTypes = {
  onScan: React.PropTypes.func,
  onStartScan: React.PropTypes.func,
  onEventTrack: React.PropTypes.func,
  buttonProps: React.PropTypes.object,
  text: React.PropTypes.string,
};

export default KeDaScan
